﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.Web.Html;

namespace ERPStore.Web.Services
{
	public class RewriterService : IRewriterService
	{
		public RewriterService(ERPStore.Services.ICatalogService catalogService
			, EventBrokR.IPublisher publisher
			, ERPStore.Logging.ILogger logger)
		{
			this.CatalogService = catalogService;
			this.EventPublisher = publisher;
			this.Logger = logger;
		}

		protected ERPStore.Services.ICatalogService CatalogService { get; private set; }
		protected EventBrokR.IPublisher EventPublisher { get; private set; }
		protected ERPStore.Logging.ILogger Logger { get; private set; }

		#region IRewriterService Members

		public virtual ERPStore.Models.Product RecoverableProduct(string code)
		{
			return null;
		}

		public virtual ERPStore.Models.ProductCategory RecoverableProductCategory(string productCategoryLink)
		{
			return null;
		}

		public virtual ActionResult RewriteUrl(System.Web.HttpRequestBase Request)
		{
			Logger.Warn("Page not found : {0} \r\nReferer: {1}User Agent: \r\n {2}User Address \r\n {3}\r\nStack: {4}", 
				Request.RawUrl, 
				Request.UrlReferrer,
				Request.UserAgent, 
				Request.UserHostAddress,
				System.Environment.StackTrace);

			var result = new ViewResult();
			result.ViewName = "404";
			return result;
		}

		public virtual ActionResult RewriteBrandUrl(ERPStore.Models.Brand brand, System.Web.Mvc.UrlHelper helper)
		{
			if (helper.RequestContext.HttpContext.Request["emptybrand"] != null)
			{
				return new EmptyResult(); // Prevent redirect loop
			}

			Logger.Debug("Empty Brand : {0} \r\nReferer: {1}User Agent: \r\n {2}User Address \r\n {3}",
				helper.RequestContext.HttpContext.Request.RawUrl,
				helper.RequestContext.HttpContext.Request.UrlReferrer,
				helper.RequestContext.HttpContext.Request.UserAgent,
				helper.RequestContext.HttpContext.Request.UserHostAddress);

			var url = helper.ShortSearchHref(brand.Name);
			url = url.AddParameter("from", "emptybrand");
			return new RedirectResult(url);
		}

		public virtual ActionResult RewriteProductUrl(ERPStore.Models.Product product, UrlHelper helper)
		{
			return new EmptyResult();
		}

		public ActionResult RewriteProductCategoryUrl(ERPStore.Models.ProductCategory category, UrlHelper helper)
		{
			if (helper.RequestContext.HttpContext.Request["emptycategory"] != null)
			{
				return new EmptyResult(); // Prevent redirect loop
			}

			Logger.Debug("Empty Category : {0} \r\nReferer: {1}User Agent: \r\n {2}User Address \r\n {3}",
				helper.RequestContext.HttpContext.Request.RawUrl,
				helper.RequestContext.HttpContext.Request.UrlReferrer,
				helper.RequestContext.HttpContext.Request.UserAgent,
				helper.RequestContext.HttpContext.Request.UserHostAddress);

			var url = helper.ShortSearchHref(category.Name);
			url = url.AddParameter("from", "emptycategory");
			return new RedirectResult(url);
		}

		#endregion
	}
}
